import { parse } from 'url';
import { TableListItem, TableListParams } from './data.d';

const courier_name = ['顺丰速递', '申通速运', '圆通快递', '德邦物流', '韵达快递', '邮政速运'];

function getCourierCompanyList(
  req: { url: any },
  res: {
    json: (
      arg0: { body: { list: TableListItem[]; count: number; } },
    ) => void;
  },
  u: any,
) {
  let new_url = u;
  if (!new_url || Object.prototype.toString.call(new_url) !== '[object String]') {
    new_url = req.url;
  }

  const count = 50;
  const params = (parse(new_url, true).query as unknown) as TableListParams;

  const { pageIndex, pageSize } = params;
  const starNum = (pageIndex - 1) * pageSize;
  const endNum = pageIndex * pageSize;

  const dataSource: TableListItem[] = [];

  [...new Array(endNum - starNum)].map((item, index) => {
    dataSource.push({
      id: starNum + index + 1,
      name: courier_name[Math.floor(Math.random() * 6)],
      telephone: '010-34567890',
      expRule: '^\\d{12}$',
      scope: 3,
      scopeName: '运输、配送、仓储、包装、搬运装卸、流通制作',
      scopeArray: [],
      scopeType: [],
      status: Math.floor(Math.random() * 2),
      img: '',
      createUser: 3520,
      createUserName: '张三丰',
      createTime: '2010-11-20',
      cainiaoCode: '',
      cainiaoFlag: 0,
      canElectronic: 0,
      display: 6,
      displayArray: [1, 2],
      displayType: [1],
      exp100Code: '',
      file: '',
      kdniaoCode: '',
      level: 1,
      symEshop: [],
      templateType: '',
      updateTime: '2019-05-05 14:53:25',
      updateUser: 2245,
      updateUserName: '张三丰',
    });
    return dataSource;
  });

  const result = {
    code: 0,
    body: {
      list: dataSource,
      count,
    },
  };

  return res.json(result);
}

export default {
  'GET /express/list': getCourierCompanyList,
};
